﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class SubstituteBLL : IBaseInfoDetail
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;
        private static readonly ISubstituteSkuDA substituteDA = DataAccess.CreateSubstituteSkuDA();

        public SubstituteBLL()
        {
            this.pubParasInfo = null;
        }

        public SubstituteBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public SubstituteBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string DeleteDetail(List<string> ownerID, List<string> skuID, List<string> substituteSkuID)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                int i;
                if (ownerID.Count != skuID.Count)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2210_017", this.pubParasInfo.UserLanguage);
                }
                List<SubstituteSkuInfo> substituteSkuInfos = new List<SubstituteSkuInfo>();
                SubstituteSkuInfo substituteSkuInfo = null;
                for (i = 0; i < ownerID.Count; i++)
                {
                    substituteSkuInfo = substituteDA.GetSubstituteSkuByID(dataBase, tran, ownerID[0], skuID[0], substituteSkuID[0], this.dataBaseID[0]);
                    substituteSkuInfos.Add(substituteSkuInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (substituteDA.DeleteSubstituteSku(dataBase, tran, ownerID, skuID, substituteSkuID, this.dataBaseID) > 0)
                {
                    for (i = 0; i < ownerID.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<SubstituteSkuInfo>(dataBase, tran, substituteSkuInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "SUBSTITUTE_SKU", this.dataBaseID);
                        string CS00002 = temp;
                        temp = CS00002 + ownerID[i] + "," + skuID[i] + "," + substituteSkuID[i] + ";";
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllDetailInfo()
        {
            return substituteDA.GetAllSubstituteSku(this.dataBaseID[0]);
        }

        public DataSet GetDetaiInfolByQueryEntity(object obj)
        {
            SubstituteSkuQueryEntity substituteSkyQuery = obj as SubstituteSkuQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(substituteSkyQuery.SkuID))
            {
                sqlWhere.Add(" SKU_ID " + substituteSkyQuery.SkuID);
            }
            return substituteDA.GetSubstituteSkuByQueryList(sqlWhere, substituteSkyQuery, this.dataBaseID[0]);
        }

        public object GetDetailInfoByID(string ownerID, string skuID, string substituteSkuID)
        {
            return substituteDA.GetSubstituteSkuByID(ownerID, skuID, substituteSkuID, this.dataBaseID[0]);
        }

        public List<object> GetDetailInfoListByID(string ownerID, string skuID)
        {
            List<SubstituteSkuInfo> substituteSkuInfos = substituteDA.GetSubstituteSkuListByID(ownerID, skuID, this.dataBaseID[0]);
            List<object> objs = null;
            if ((substituteSkuInfos != null) && (substituteSkuInfos.Count > 0))
            {
                objs = new List<object>();
                for (int i = 0; i < substituteSkuInfos.Count; i++)
                {
                    objs.Add(substituteSkuInfos[i]);
                }
            }
            return objs;
        }

        public string InsertDetail(object obj)
        {
            SubstituteSkuInfo substituteSkuInfo = obj as SubstituteSkuInfo;
            string substituteSkuID = string.Empty;
            try
            {
                if (substituteSkuInfo == null)
                {
                    return substituteSkuID;
                }
                if (substituteDA.CheckSubstituteSkuIDUnique(substituteSkuInfo.OwnerID, substituteSkuInfo.SkuID, substituteSkuInfo.SubstituteSkuID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2210_016", this.pubParasInfo.UserLanguage, new List<string> { substituteSkuInfo.SubstituteSkuID });
                }
                substituteSkuInfo.UpdatedBy = substituteSkuInfo.CreatedBy;
                substituteSkuInfo.CreatedDate = DateTime.Now;
                substituteSkuInfo.UpdatedDate = DateTime.Now;
                if (substituteDA.InsertSubstituteSku(substituteSkuInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<SubstituteSkuInfo>(null, substituteSkuInfo, AuditAction.Insert, this.pubParasInfo.UserID, "SUBSTITUTE_SKU", this.dataBaseID[0]);
                    substituteSkuID = substituteSkuInfo.SubstituteSkuID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return substituteSkuID;
        }

        public string UpdateDetail(object obj)
        {
            string substituteSkuID = string.Empty;
            try
            {
                SubstituteSkuInfo substituteSkuInfo = obj as SubstituteSkuInfo;
                SubstituteSkuInfo oldSubstituteSkuInfo = substituteDA.GetSubstituteSkuByID(substituteSkuInfo.OwnerID, substituteSkuInfo.SkuID, substituteSkuInfo.SubstituteSkuID, this.dataBaseID[0]);
                if (substituteDA.UpdateSubstituteSku(substituteSkuInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<SubstituteSkuInfo>(oldSubstituteSkuInfo, substituteSkuInfo, AuditAction.Update, this.pubParasInfo.UserID, "SUBSTITUTE_SKU", this.dataBaseID[0]);
                    substituteSkuID = substituteSkuInfo.SubstituteSkuID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return substituteSkuID;
        }

        public string UpdateIsActive(List<string> mainID, List<string> subId, string isActive)
        {
            throw new NotImplementedException();
        }
    }
}

